package com.sdnu.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdnu.service.entity.quest.CheckInData;
import com.sdnu.service.entity.quest.StudentData;
import com.sdnu.service.entity.quest.excel.InfoFormExcel;
import com.sdnu.service.entity.quest.vo.CheckInDataVo;
import com.sdnu.service.entity.quest.vo.StudentDataVo;
import com.sdnu.service.mapper.StudentDataMapper;
import com.sdnu.service.service.StudentDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Konjacer
 * @since 2022-03-23
 */
@Service
public class StudentDataServiceImpl extends ServiceImpl<StudentDataMapper, StudentData> implements StudentDataService {

    //  生成表格数据
    public List<InfoFormExcel> getInfoFormExcel(){
        List<InfoFormExcel> formExcelList = new ArrayList<>();
        QueryWrapper<StudentData> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        List<StudentData> studentDataList = this.list(wrapper);

        for(StudentData studentData: studentDataList){
            InfoFormExcel infoFormExcel = new InfoFormExcel();
            BeanUtils.copyProperties(studentData,infoFormExcel);
            if(studentData.getSex()==0){
                infoFormExcel.setGender("男");
            }else{
                infoFormExcel.setGender("女");
            }
            switch (studentData.getPositionWant()){
                case 0:infoFormExcel.setPositionWant("会长");break;
                case 1:infoFormExcel.setPositionWant("副会长");break;
                case 2:infoFormExcel.setPositionWant("秘书长");break;
                case 3:infoFormExcel.setPositionWant("理事");break;
                default:infoFormExcel.setPositionWant("null");
            }
//            处理日期格式，不用EasyExcel框架中的了
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
            infoFormExcel.setAdmissionDate(sdf2.format(studentData.getAdmissionDate()));
            infoFormExcel.setGmtCreate(sdf1.format(studentData.getGmtCreate()));
            formExcelList.add(infoFormExcel);
        }

        return formExcelList;
    }

    @Override
    public List<StudentDataVo> getAll() {
        QueryWrapper<StudentData> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        List<StudentData> checkInDataList = this.list(wrapper);

        List<StudentDataVo> finalList = new ArrayList<>();
        for(StudentData item : checkInDataList){
            StudentDataVo listVo = new StudentDataVo();
            BeanUtils.copyProperties(item,listVo);
            finalList.add(listVo);
        }

        return finalList;
    }

    @Override
    public List<StudentDataVo> pageSelect(Page<StudentData> page, QueryWrapper<StudentData> wrapper) {
        this.page(page,wrapper);
        List<StudentData> records = page.getRecords();
        long total = page.getTotal();

        List<StudentDataVo> finalList = new ArrayList<>();
        for(StudentData item : records){
            StudentDataVo listVo = new StudentDataVo();
            BeanUtils.copyProperties(item,listVo);
            finalList.add(listVo);
        }
        return finalList;
    }

    @Override
    public QueryWrapper<StudentData> querySelect(StudentDataVo studentDataVo) {
            QueryWrapper<StudentData> wrapper = new QueryWrapper<>();
            String name = studentDataVo.getName();
            Integer sex = studentDataVo.getSex();
            Date admission = studentDataVo.getAdmissionDate();
            String major = studentDataVo.getMajor();
            String phone = studentDataVo.getPhone();
            String organization = studentDataVo.getWorkOrganization();
            String positionNow = studentDataVo.getPositionNow();
            Integer positionWant = studentDataVo.getPositionWant();

            //判断条件值是否为空，如果不为空则拼接条件
            if(!StringUtils.isEmpty(name)){
                wrapper.like("name",name);
            }
            if(!StringUtils.isEmpty(sex)){
                //构造条件
                wrapper.eq("sex",sex);
            }
            if(!StringUtils.isEmpty(admission)){
                //构造条件
                wrapper.like("admission_date",admission);
            }
            if(!StringUtils.isEmpty(major)){
                //构造条件
                wrapper.like("major",major);
            }
            if(!StringUtils.isEmpty(phone)){
                //构造条件
                wrapper.like("phone",phone);
            }
            if(!StringUtils.isEmpty(organization)){
                //构造条件
                wrapper.like("work_organization",organization);
            }
            if(!StringUtils.isEmpty(positionNow)){
                //构造条件
                wrapper.like("position_now",positionNow);
            }
            if(!StringUtils.isEmpty(positionWant)){
                //构造条件
                wrapper.eq("position_want",positionWant);
            }

            return wrapper;
        }
}
